package com.yikaiye.android.yikaiye.ui.fragment;

import android.view.View;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseFragment;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.databinding.FragmentServiceProgressBinding;
import com.yikaiye.android.yikaiye.model.ServiceDetailsModel;
import com.yikaiye.android.yikaiye.ui.adapter.NodeProgressAdapter;
import com.yikaiye.android.yikaiye.widget.AutoHeightViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务进度
 */
public class ServiceProgressFragment extends BaseFragment<FragmentServiceProgressBinding, BaseViewModel> {

    private final AutoHeightViewPager viewPager;
    private final List<ServiceDetailsModel.ServiceProgressModel> serviceProgressList;
    private OptionsPickerView<String> pvOptions;
    private NodeProgressAdapter adapter;
    private final List<String> options1Items = new ArrayList<>();
    private final int currentPeriod;
    private final int totalPeriod;
    private final int serviceType;

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initData() {
        viewPager.setObjectForPosition(binding.getRoot(), 0);//0代表tab的位置
        if (serviceProgressList != null && !serviceProgressList.isEmpty()) {
            if (serviceProgressList.get(0).getStatus() == 0) {
                binding.serviceNotStartLl.setVisibility(View.VISIBLE);
                binding.llChooseTerm.setVisibility(View.GONE);
                binding.rv.setVisibility(View.GONE);
                return;
            }
        }
        adapter = new NodeProgressAdapter(mContext);
        binding.rv.setAdapter(adapter);
        // 服务类型：0.交付 1.周期
        if (serviceType == 0) {
            binding.llChooseTerm.setVisibility(View.GONE);
            if (serviceProgressList != null && !serviceProgressList.isEmpty()) {
                adapter.submitList(serviceProgressList.get(0).getServiceNodeList());
                adapter.setStatus(serviceProgressList.get(0).getStatus());
            }
        } else {
            if (totalPeriod == 1) {
                binding.llChooseTerm.setVisibility(View.GONE);
                if (serviceProgressList != null && !serviceProgressList.isEmpty()) {
                    adapter.submitList(serviceProgressList.get(0).getServiceNodeList());
                    adapter.setStatus(serviceProgressList.get(0).getStatus());
                }
            } else if (totalPeriod > 1) {
                binding.llChooseTerm.setVisibility(View.VISIBLE);
                binding.tvCurrentTerm.setText(getString(R.string.service_node_cycle2, currentPeriod));
                if (serviceProgressList != null && !serviceProgressList.isEmpty()) {
                    adapter.submitList(serviceProgressList.get(currentPeriod - 1).getServiceNodeList());
                    adapter.setStatus(serviceProgressList.get(currentPeriod - 1).getStatus());
                }
                for (int i = 0; i < totalPeriod; i++) {
                    options1Items.add(getString(R.string.period_count, (i + 1)));
                }
                initOptionPicker();
            }
        }
    }

    @Override
    protected void initClick() {
        viewModel.onDelayClick(binding.llChooseTerm, o -> pvOptions.show());
    }

    public ServiceProgressFragment(AutoHeightViewPager viewPager, List<ServiceDetailsModel.ServiceProgressModel> serviceProgressList, int currentPeriod, int totalPeriod, int serviceType) {
        this.viewPager = viewPager;
        this.serviceProgressList = serviceProgressList;
        this.currentPeriod = currentPeriod;
        this.totalPeriod = totalPeriod;
        this.serviceType = serviceType;
    }

    public static ServiceProgressFragment newInstance(AutoHeightViewPager viewPager, List<ServiceDetailsModel.ServiceProgressModel> serviceProgressList, int currentPeriod, int totalPeriod, int serviceType) {
        return new ServiceProgressFragment(viewPager, serviceProgressList, currentPeriod, totalPeriod, serviceType);
    }

    private void initOptionPicker() {
        pvOptions = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            if (!serviceProgressList.isEmpty()) {
                if (serviceProgressList.get(options1).getStatus() == 0) {
                    binding.serviceNotStartLl.setVisibility(View.VISIBLE);
                    binding.llChooseTerm.setVisibility(View.VISIBLE);
                    binding.rv.setVisibility(View.GONE);
                } else {
                    binding.serviceNotStartLl.setVisibility(View.GONE);
                    binding.llChooseTerm.setVisibility(View.VISIBLE);
                    binding.rv.setVisibility(View.VISIBLE);
                    adapter.submitList(serviceProgressList.get(options1).getServiceNodeList());
                    adapter.setStatus(serviceProgressList.get(options1).getStatus());
                }
                binding.tvCurrentTerm.setText(getString(R.string.service_node_cycle2, options1 + 1));
            }
        }).setTitleText(getString(R.string.choose_period)).setLineSpacingMultiplier((float) 2.6).setSubmitText(getString(R.string.finish_text)).setCancelColor(0xFF666666).setTextColorCenter(0xFF000000).setContentTextSize(17).setTitleSize(18).build();
        pvOptions.setPicker(options1Items);
    }
}
